package com.topone.service.impl;

import com.topone.Enum.ResultEnum;
import com.topone.Exception.FanZoneException;
import com.topone.dataObject.Mapper.UserCouponMapper;
import com.topone.dataObject.TcUserCoupon;
import com.topone.dto.BussinessDTO;
import com.topone.dto.TcUserCouponDTO;
import com.topone.repository.TcUserCouponRepository;
import com.topone.service.TcUserCouponService;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/6,0006.
 */
@Service
@Slf4j
public class TcUserCouponServiceImpl implements TcUserCouponService {
    @Autowired
    private TcUserCouponRepository repository;

    @Autowired
    private UserCouponMapper userCouponMapper;

    public List<TcUserCoupon> findUserConpon(String userId, String status) {
        List<TcUserCoupon> userCouponList = new ArrayList<>();
        userCouponList = repository.findByStatusEquals(status);
        return userCouponList;
    }

    //根据用openid查询优惠券信息
    @Override
    public List<BussinessDTO> findCouponByopenid(TcUserCoupon tcUserCoupon) {
        List<BussinessDTO> bussinessDTOList = new ArrayList<>();
        try {
            bussinessDTOList = userCouponMapper.findCouponByopenid(tcUserCoupon);
        } catch (Exception e) {
            log.error("【查询个人优惠券】失败,{}", tcUserCoupon);
            e.printStackTrace();
            throw new FanZoneException(ResultEnum.FIND_USERCOUPON_ERROR);

        }
        return bussinessDTOList;
    }

    //查询所有未支付的优惠券订单
    @Override
    public List<TcUserCouponDTO> findAllUserCoupon(Integer status) {
        return userCouponMapper.findUserCoupon(status);
    }

    //更新用户超时未支付订单回退
    @Override
    @Transactional
    public void cancelUserCoupon(String id) {
        userCouponMapper.cancelUserCoupon(id);
    }

    //商家核销券, 查询券

    @Override
    public List<TcUserCouponDTO> findUserCouponList(String userPhone, String userCouponId) {
        return userCouponMapper.findUserCouponList(userPhone, userCouponId);
    }

    @Override
    @Transactional
    @Synchronized
    public String updateUserCouponById(String userCouponId) {
        if (userCouponMapper.countUserCoupon(userCouponId) == 1) {
            userCouponMapper.updateuserCoupon(userCouponId);
            return "success";
        } else {
            return "error";
        }
    }

    //查询所有用户超时未使用优惠券
    @Override
    public List<TcUserCouponDTO> findUserCouponOvertime(Integer status) {
        List<TcUserCouponDTO> list = userCouponMapper.findUserCouponOverTime(status);
        try {
            for (int i = 0; i < list.size(); i++) {
                cancelUserCouponUnuse(list.get(i).getId());
                log.info("更新用户过期优惠券成功,id={}",
                        list.get(i).getId());
            }
        } catch (FanZoneException e) {
            log.error("处理用户超时优惠券失败,{}", e.getMessage());
            e.printStackTrace();
        }
        return list;
    }

    //处理用户超时未使用的优惠券
    @Override
    public void cancelUserCouponUnuse(String id) {
         userCouponMapper.cancelUserCouponOverTime(id);
    }
}
